package com.ruoyi.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.InvoiceRecords;
import com.ruoyi.system.domain.InvoiceRecordsAudit;
import com.ruoyi.system.domain.SysAudit;
import com.ruoyi.system.service.impl.InvoiceRecordsAuditServiceImpl;
import com.ruoyi.system.service.impl.InvoiceRecordsServiceImpl;
import com.ruoyi.system.service.impl.SysAuditServiceImpl;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Slf4j
public class NoModelDataListener2 extends AnalysisEventListener<Map<Integer, String>> {




    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private InvoiceRecordsServiceImpl invoiceRecordsService;
    private SysDeptServiceImpl sysDeptService;
    private InvoiceRecordsAuditServiceImpl invoiceRecordsAuditService;
    private SysAuditServiceImpl sysAuditService;
    private String time;

    public NoModelDataListener2(InvoiceRecordsServiceImpl invoiceRecordsService,SysDeptServiceImpl sysDeptService,InvoiceRecordsAuditServiceImpl invoiceRecordsAuditService,SysAuditServiceImpl sysAuditService,String time){
        this.invoiceRecordsService = invoiceRecordsService;
        this.sysDeptService= sysDeptService;
        this.invoiceRecordsAuditService = invoiceRecordsAuditService;
        this.sysAuditService = sysAuditService;
        this.time= time;
    }

    @SneakyThrows
    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
//        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData(cachedDataList);
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }


    @SneakyThrows
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData(cachedDataList);
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库-金山
     */
    private void saveData(List<Map<Integer, String>> cachedDataList)throws IOException {
//        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<Integer,String> map:cachedDataList) {
                log.info(map.toString());
                InvoiceRecords invoiceRecords = new InvoiceRecords();
                SysAudit sysAudit = new SysAudit();
                sysAudit.setProductCode(map.get(0));
                 List<SysAudit> sysAudits = sysAuditService.selectSysAuditList(sysAudit);
                    if(sysAudits==null||sysAudits.size()==0){
                    invoiceRecords.setProductCode(map.get(0));
                    invoiceRecords.setProductName(map.get(1));
                    invoiceRecords.setModel(map.get(2));
                    invoiceRecords.setSpecs(map.get(3));
                    if (map.get(11).equals("1")) {
                        invoiceRecords.setSettlementMode("压库");
                    } else if (map.get(11).equals("2")) {
                        invoiceRecords.setSettlementMode("买断");
                    }
                    invoiceRecords.setProductBrand(map.get(4));
                    String provide = map.get(5);
                    SysDept sysDept = sysDeptService.selectDeptByName(provide);
                    invoiceRecords.setProvide(map.get(5));
                    if (sysDept != null) {
                        invoiceRecords.setProvide(map.get(5));
                    } else {
                        throw new IOException("有不存在的供应商账号-" + provide + "请先添加账号再导入！");
                    }
                    invoiceRecords.setProductCount(map.get(8));
                    invoiceRecords.setProductPrice(map.get(9));
                    invoiceRecords.setPlarnPrice(map.get(10));
                    String company = map.get(14);
                    invoiceRecords.setCompany(company);
                    invoiceRecords.setCreateTime(time);
                    DecimalFormat df = new DecimalFormat("0.00##");
                    invoiceRecords.setProductMoney(df.format(Double.valueOf(map.get(8)) * Double.valueOf(map.get(10))));
                    invoiceRecords.setStatus("0");
                    invoiceRecordsService.insertInvoiceRecords(invoiceRecords);
                }else {
                    InvoiceRecordsAudit invoiceRecordsAudit = new InvoiceRecordsAudit();
                    invoiceRecordsAudit.setProductCode(map.get(0));
                    invoiceRecordsAudit.setProductName(map.get(1));
                    invoiceRecordsAudit.setModel(map.get(2));
                    invoiceRecordsAudit.setSpecs(map.get(3));
                    if (map.get(11).equals("1")) {
                        invoiceRecordsAudit.setSettlementMode("压库");
                    } else if (map.get(11).equals("2")) {
                        invoiceRecordsAudit.setSettlementMode("买断");
                    }
                    invoiceRecordsAudit.setProductBrand(map.get(4));
                    String provide = map.get(5);
                    SysDept sysDept = sysDeptService.selectDeptByName(provide);
                    invoiceRecordsAudit.setProvide(map.get(5));
                    if (sysDept != null) {
                        invoiceRecordsAudit.setProvide(map.get(5));
                    } else {
                        throw new IOException("有不存在的供应商账号-" + provide + "请先添加账号再导入！");
                    }
                    invoiceRecordsAudit.setProductCount(map.get(8));
                    invoiceRecordsAudit.setProductPrice(map.get(9));
                    invoiceRecordsAudit.setPlarnPrice(map.get(10));
                    String company = map.get(14);
                    invoiceRecordsAudit.setCompany(company);
                    invoiceRecordsAudit.setCreateTime(time);
                    DecimalFormat df = new DecimalFormat("0.00");
                    invoiceRecordsAudit.setProductMoney(df.format(Double.valueOf(map.get(8)) * Double.valueOf(map.get(10))));
                    invoiceRecordsAudit.setStatus("0");
                    invoiceRecordsAuditService.insertInvoiceRecordsAudit(invoiceRecordsAudit);
                }
            }
        log.info("存储数据库成功！");
    }
}